package com.self.cgqcompose.ui.components.areacode

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.self.cgqcompose.beans.AreaBlock
import com.self.cgqcompose.mock.MockJson
import kotlinx.coroutines.delay

@Composable
fun AreaCode1(data: AreaBlock){
    val pagerState = rememberPagerState { data.data.data.size }
    var isAutoScrolling by remember { mutableStateOf(true) }
    val scope = rememberCoroutineScope()

    // 自动轮播逻辑
    LaunchedEffect(Unit) {
            if (isAutoScrolling && data.data.data.isNotEmpty()) {
                while (true) {
                    delay(3000L) // 每 3 秒切换一次
                    val nextPage = (pagerState.currentPage + 1) % data.data.data.size
                    pagerState.animateScrollToPage(nextPage)
                }
            }
    }

    Box {
        HorizontalPager(pagerState) { page ->
            AsyncImage(
                model = data.data.data[page].cover, // 假设 tab 对象有一个 iconUrl 属性
                contentDescription = "tab icon",
                modifier = Modifier
                    .padding(3.dp)
                    .fillMaxWidth()
                    .aspectRatio(16f/9f)
                    .clip(RoundedCornerShape(5.dp))
            )
        }

        // 指示器
        Row(
            modifier = Modifier.align(Alignment.BottomEnd)
                .padding(8.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            repeat(data.data.data.size) { index ->
                Box(
                    modifier = Modifier
                        .size(if(pagerState.currentPage == index) 8.dp else 6.dp)
                        .clip(CircleShape)
                        .background(
                            if (pagerState.currentPage == index) Color.White else Color.White.copy(alpha = 0.7f)
                        )
                )
            }
        }
    }
}

@Preview
@Composable
fun AreaCode1Preview(){
    val context = LocalContext.current;
    val area9 = MockJson.loadMockDataArea9(context)
    AreaCode1(area9.data[0])
}